package client.states;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import client.Creature;
import client.GraphicsFormatter;
import client.IGameState;
import client.Main;
import client.Player;
import client.PriorityMessageQueue;
import client.StateContext;
import client.World;
import client.XMLReader;
import client.XMLLoaders.CreatureLoader;

import com.jpemartin.jgt.KeyboardStatus;

public class CreatureSelectScreen implements IGameState {

	private String text;
	private GraphicsFormatter formatter;
	private List<Creature> startingCreatures;
	private String startingCreaturesPath;
	private String cutscenePath;
	boolean showCreatures = false;
	private int selectedIndex = 0;

	public CreatureSelectScreen() {
		XMLReader reader = new XMLReader();
		cutscenePath = "OpeningCutscene.xml";
		text = reader.readCutscene(cutscenePath);
		formatter = new GraphicsFormatter();
		
		
	}

	@Override
	public void paint(Graphics g, StateContext stateContext, int GameTime) {
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, Main.staticWidth(), Main.staticHeight());
		if (showCreatures) {
			g.setColor(Color.GREEN);
			g.fillRect(0, selectedIndex * 50, 200, 50);
			g.setColor(Color.BLACK);
			int x = 0;
			int y = 0;
			int i = 0;
			for (Creature c : startingCreatures) {
				c.displayStats(g, x, y);
				if (selectedIndex == i) {
					c.displayMoreStats(g, 200, y);
				}
				y += 50;
				i++;
			}
		} else {

			g.setColor(Color.BLACK);
			g.fillRect(0, 350, 400, 50);
			formatter.drawLongString(25, 50, 50, text, g);
			g.setColor(Color.WHITE);
			g.drawString("Press Enter to continue", 100, 375);
		}
	}

	public void initGame(StateContext stateContext) {
		Player player = new Player();
		
		Creature selectedCreature = startingCreatures.get(selectedIndex);
		player.addCreature(selectedCreature);
		World world = new World();
		ExploreScreen explore = new ExploreScreen(player, world);
		player.setDefaultColor(selectedCreature.getColor());
		stateContext.setState(explore);
	}

	@Override
	public void processKeyHit(StateContext stateContext, KeyboardStatus status) {
		if (status.key[KeyEvent.VK_ENTER]) {
			status.key[KeyEvent.VK_ENTER] = false;
			if (!showCreatures) {
				showCreatures = true;
				startingCreaturesPath = "StartingCreatures.xml";
				startingCreatures = new ArrayList<Creature>();
				CreatureLoader loader = new CreatureLoader(startingCreaturesPath);
				startingCreatures = loader.getCreatures();
			} else {
				initGame(stateContext);
				PriorityMessageQueue.addMessage("Welcome");
			}
		}
		if (showCreatures) {
			if (status.key[KeyEvent.VK_RIGHT]) {
				if (selectedIndex < startingCreatures.size() - 1) {
					selectedIndex++;
					status.key[KeyEvent.VK_RIGHT] = false;
				}
			} else if (status.key[KeyEvent.VK_DOWN]) {
				if (selectedIndex < startingCreatures.size() - 1) {
					selectedIndex++;
					status.key[KeyEvent.VK_DOWN] = false;
				}
			} else if (status.key[KeyEvent.VK_LEFT]) {
				if (selectedIndex > 0) {
					selectedIndex--;
					status.key[KeyEvent.VK_LEFT] = false;
				}
			} else if (status.key[KeyEvent.VK_UP]) {
				if (selectedIndex > 0) {
					selectedIndex--;
					status.key[KeyEvent.VK_UP] = false;
				}
			}
		}
	}

}
